package com.sourcetrip.my.modle;

/**
 * @author: ZhouBert
 * @date: 2020/12/16
 * @description: AVLTreeV0 节点
 * AVL 树
 */
public class AVLTreeNode<E> extends TreeNode<E> {

	/**
	 * 节点的高度
	 */
	public int height = 0;

	public AVLTreeNode(E element, TreeNode<E> parent) {
		super(element, parent);
	}

	/**
	 * 重置本节点的 height
	 * （不重置 左/右子节点，而是直接重置）
	 */
	public void resetHeight() {
		int leftHeight = left == null ? 0 : ((AVLTreeNode) left).height;
		int rightHeight = right == null ? 0 : ((AVLTreeNode) right).height;
		height = Math.max(leftHeight, rightHeight) + 1;
	}

	/**
	 * 判断本节点是否平衡
	 * 直接从 左右节点 进行判断
	 *
	 * @return true:is balance
	 */
	public boolean isBalance() {
		int leftHeight = left == null ? 0 : ((AVLTreeNode) left).height;
		int rightHeight = right == null ? 0 : ((AVLTreeNode) right).height;
		return Math.abs(leftHeight - rightHeight) < 2;
	}

	/**
	 * 判断是否左子树更高
	 * 用于删除节点时
	 */
	public boolean isLeftHigher() {
		int leftHeight = left == null ? 0 : ((AVLTreeNode) left).height;
		int rightHeight = right == null ? 0 : ((AVLTreeNode) right).height;
		return leftHeight - rightHeight > 0;
	}
}
